package com.leyou.sms.mq;

import com.leyou.sms.config.SmsPropertie;
import com.leyou.sms.utils.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @author huangxin
 * @date 2019/5/10-21:48
 * 短信验证监听器
 */
@Slf4j
@Component
public class SmsListener {
    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private SmsPropertie smsPropertie;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "sms.verify.code.queue", durable = "true"),
            exchange = @Exchange(
                    value = "ly.sms.exchange",
                    //忽略声明异常
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"sms.verify.code"}))
    public void listenInsertOrUpdate(Map<String,String > msg) {
        if (CollectionUtils.isEmpty(msg)){
            return;
        }
        String phone = msg.get("phone");
        if (StringUtils.isBlank(phone)){
            return;
        }
        String code = msg.get("code");
        smsUtils.send(phone,smsPropertie.getSmsSign(),smsPropertie.getTemplateId(),code,"5");

        log.info("[短信服务], 发送短信验证码,手机号:{}",phone);
    }
}
